#include "Configuration.hpp"
#include "Debug.hpp"
#include "CacheManager.hpp"
#include "SearchEngineServer.hpp"
#include "Thread.hpp"

#include <unistd.h>

#include <iostream>
#include <fstream>

/**
 * @brief class SearchEngineServer test function
 * 
 */
void searchEngineServerTest() {
    DEBUG_PRINT("Search engine application starts running.");
    se::Singleton<se::Dictionary>::getInstance();
    // 自动更新缓存线程
    se::Thread updateCache([] {
        int delay = CONFIGURATION("AutoUpdateCacheDelay", int);
        while (true) {
            ::sleep(delay);
            se::Singleton<se::CacheManager>::getInstance().updateCache();
            DEBUG_PRINT("\e[031;1mrefresh success\e[0m.");
        }
    });

    // 自动持久化缓存线程
    se::Thread saveCache([] {
        int delay = CONFIGURATION("AutoSaveCacheDelay", int);
        while (true) {
            ::sleep(delay);
            se::Singleton<se::CacheManager>::getInstance().saveCacheToFile();
            DEBUG_PRINT("save to file success.");
        }
    });

    updateCache.start();
    saveCache.start();

    se::SearchEngineServer searchEngineServer;
    searchEngineServer.start();
}

int main(int argc, char **argv) {
    searchEngineServerTest();
    return 0;
}
